/* Source for this snippet
 *  = http://www.codeproject.com/Articles/4755/Input-mask-a-script-that-automatically-enters-a-sp
 */

function mask(str,textbox,loc,delim){
var locs = loc.split(',');

for (var i = 0; i <= locs.length; i++){
	for (var k = 0; k <= str.length; k++){
	 if (k == locs[i]){
	  if (str.substring(k, k+1) != delim){
	   if (event.keyCode != 8){ //backspace
	    str = str.substring(0,k) + delim + str.substring(k,str.length);
       }
	  }
	 }
	}
 }
textbox.value = str
}


$(document).ready(function() {
	
	
	
	$.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Please check your input."
	);
	// validate signup form on keyup and submit
	var validator = $("#registerForm").validate({
		rules: {
			username: {
				minlength: 6
			},
			password: {
				rangelength: [8,16],
				regex: "(?=^.{8,16}$)((?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]))^.*"
			},
			phone: {
				rangelength: [14,14],
				regex: "^1.*"
			}
		},
		messages: {
			username: {
				required: "Provide a username",
				rangelength: jQuery.format("Enter at least {0} characters")
			},
			password: {
				required: "Provide a password",
				regex: "At least one uppercase and one digit.",
				rangelength: jQuery.format("Enter between {0} and {1} characters")
			},
			phone: {
				rangelength: "Please enter a valid phone number",
				regex: "Please provide a valid North American phone number"
			}
			
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			error.appendTo( element.parent());
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
		highlight: function(element, errorClass) {
			$(element).siblings('label').removeClass('checked')
		}
	})
});
